package com.promfy.anonymousshare.service;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import com.promfy.anonymousshare.domain.Login;

public class LoginManager {
	
	private Context mContext;

	public LoginManager(Context mContext) {
		super();
		this.mContext = mContext;
	}
	
	/**
	 * save login info 
	 * @param userName
	 * @param password
	 * @param isRememberPassword
	 */
	public void login(String userName, String password, boolean isRememberPassword){
		SharedPreferences sp = mContext.getSharedPreferences("annoymous", Context.MODE_PRIVATE);
		Editor editor = sp.edit();
		editor.putString("user_name", userName);
		if(isRememberPassword){
			editor.putString("password", password);
		}else{
			editor.putString("password", "");			
		}
		editor.putBoolean("is_remember_password", isRememberPassword);
		editor.commit();
	}
	
	/**
	 * get login
	 * @return
	 */
	public Login getLogin(){
		Login login = null;
		SharedPreferences sp = mContext.getSharedPreferences("annoymous", Context.MODE_PRIVATE);
		String userName = sp.getString("user_name", null);
		if(userName != null && userName.trim().length() != 0){
			boolean isRememberPassword = sp.getBoolean("is_remember_password", false);
			
			if(isRememberPassword){
				String password = sp.getString("password", null);
				login = new Login(userName, password, isRememberPassword);
			}else{
				login = new Login(userName, "", isRememberPassword);
			}
		}
		return login;
	}
}
